package com.yellowpage.ndc.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ndc.data.Classification;
import com.yellowpage.ndc.service.ClassificationService;

public class ClassificationAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<Classification> classificationList = new ArrayList<Classification>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private Classification classification;
	private ClassificationService classificationService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_line;
		private String query_classId;
		private String query_engName;
		private String query_pyName;
		private String query_bxName;
		private String query_note;
		private String query_name;
		private String query_status;
		private String    query_code;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.classification = classificationService.getClassification(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("line", query_line);
	    	    queryMap.put("classId", query_classId);
	    	    queryMap.put("engName", query_engName);
	    	    queryMap.put("pyName", query_pyName);
	    	    queryMap.put("bxName", query_bxName);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("name", query_name);
	    	    queryMap.put("status", query_status);
	    	    queryMap.put("code", query_code);
	    	    	
		resultList = classificationService.queryClassificationByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		classificationService.saveClassification(classification);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
		if (classification.getLine().length() == 0) {
			this.addActionError("** Line 不能为空 !");
		}
		if (classification.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (classification.getCode().length() == 0) {
			this.addActionError("** Code 不能为空 !");
		}

		this.setValidatorResult("/ndc/ClassificationAdd.jsp");	
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    classification = classificationService.getClassification(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    classificationService.updateClassification(classification);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
		if (classification.getLine().length() == 0) {
			this.addActionError("** Line 不能为空 !");
		}
		if (classification.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (classification.getCode().length() == 0) {
			this.addActionError("** Code 不能为空 !");
		}
		this.setValidatorResult("/ndc/ClassificationUpdate.jsp");
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.classificationService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setClassification(Classification classification) {
		this.classification = classification;
	}
	
	public Classification getClassification() {
		return classification;
	}
	 
	 
	 public void setClassificationService(ClassificationService classificationService) {
		this.classificationService = classificationService;
	}
	
	public List<Classification> getClassificationList() {
		return classificationList;
	}

	public void setClassificationList(List<Classification> classificationList) {
		this.classificationList = classificationList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_line() {
        return this.query_line;
    }
    
    public void setQuery_line(String line){
        this.query_line = line;
    }    

        public String getQuery_classId() {
        return this.query_classId;
    }
    
    public void setQuery_classId(String classId){
        this.query_classId = classId;
    }    

        public String getQuery_engName() {
        return this.query_engName;
    }
    
    public void setQuery_engName(String engName){
        this.query_engName = engName;
    }    

        public String getQuery_pyName() {
        return this.query_pyName;
    }
    
    public void setQuery_pyName(String pyName){
        this.query_pyName = pyName;
    }    

        public String getQuery_bxName() {
        return this.query_bxName;
    }
    
    public void setQuery_bxName(String bxName){
        this.query_bxName = bxName;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_name() {
        return this.query_name;
    }
    
    public void setQuery_name(String name){
        this.query_name = name;
    }    

        public String getQuery_status() {
        return this.query_status;
    }
    
    public void setQuery_status(String status){
        this.query_status = status;
    }    

        public String getQuery_code() {
        return this.query_code;
    }
    
    public void setQuery_code(String code){
        this.query_code = code;
    }    

    
	
}
